package com.integrated;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {
        Scanner sc;
        boolean flag = true;
        ArrayList<Student> list = new ArrayList<>();
        while (flag) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            System.out.println("请输入您的选择：");
            sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1":
                    add(list);
                    break;
                case "2":
                    del(list);
                    break;
                case "3":
                    update(list);
                    break;
                case "4":
                    display(list);
                    break;
                case "5":
                    System.out.println("退出");
                    flag = false;
                    break;
                default:
                    System.out.println("输入错误，请重新输入！");
                    break;
            }
        }
    }

    public static boolean add(ArrayList<Student> list) {
        Student stu = new Student();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生的id：");
        stu.setId(sc.nextInt());
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId()==stu.getId()){
                System.out.println("已有为此id的学生，不可重复添加！");
                return false;
            }
        }
        System.out.println("请输入学生的姓名：");
        stu.setName(sc.next());
        System.out.println("请输入学生的年龄：");
        stu.setAge(sc.nextInt());
        System.out.println("请输入学生的家庭住址：");
        stu.setHome(sc.next());
        list.add(stu);
        System.out.println("添加成功！");
        return true;
    }

    public static void del(ArrayList<Student> list) {
        System.out.println("请输入要删除学生的id");
        Scanner sc = new Scanner(System.in);
        int del_id = sc.nextInt();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() == del_id) {
                list.remove(i);
                System.out.println("删除成功!");
                break;
            }
            if (i == list.size() - 1) {
                System.out.println("没有此id的学生");
            }
        }
    }

    public static void update(ArrayList<Student> list) {
        System.out.println("请输入要修改学生的id");
        Scanner sc = new Scanner(System.in);
        int up_id = sc.nextInt();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() == up_id) {
                System.out.println("1.id");
                System.out.println("2.姓名");
                System.out.println("3.年龄");
                System.out.println("4.家庭住址");
                System.out.println("请输入需要修改的内容：");
                String choose = sc.next();
                switch (choose) {
                    case "1":
                        System.out.println("修改id为：");
                        list.get(i).setId(sc.nextInt());
                        break;
                    case "2":
                        System.out.println("修改姓名为：");
                        list.get(i).setName(sc.next());
                        break;
                    case "3":
                        System.out.println("修改年龄为：");
                        list.get(i).setAge(sc.nextInt());
                        break;
                    case "4":
                        System.out.println("修改家庭住址为：");
                        list.get(i).setHome(sc.next());
                        break;
                    default:
                        System.out.println("修改错误！");
                }
                System.out.println("修改成功");
                break;
            }
            if (i == list.size() - 1) {
                System.out.println("没有为此id的学生，无法进行修改！");
            }
        }
    }

    public static boolean display(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("系统中没有数据，请输入后再查看");
            return false;
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println("id为：" + list.get(i).getId() + "  姓名为：" + list.get(i).getName()
                    + "  年龄为：" + list.get(i).getAge() + "  家庭住址为：" + list.get(i).getHome());
        }
        return true;
    }
}
